'''
Created on 27/03/2009

@author: eh01
'''
from xml.etree import ElementTree
from yarra.datastore import dsconstants, dsofactory
from yarra.datastore.dso import DSO
from yarra.datastore.dsstub import DSStub
from yarra.datastore.dst import DST
from yarra.event import eventconstants
from yarra.event.event import Event
from yarra.event.eventhandler import BaseEventHandler
from yarra.event.eventmanager import EventManager
from yarra.handler import messagetypeconstants
from yarra.logging import yarralogging
import httplib
import os

class ConnectionTestHTTPResponseHandler(BaseEventHandler):
    '''
    Handles the response message for the connection test
    '''

    __logger = None
    
    def __init__(self):
        '''
        Constructor
        '''
        self.__logger = yarralogging.get_logger(DSStub().get_application_name())
        
    def do(self, data):
        dso = data
        request_id = dso.get_dst(subject=dsconstants.SUBJECT_ROOT, 
                                 predicate=dsconstants.PREDICATE_REQUEST_ID).get_literal()
        ds = DSStub()
        
        result = ds.get_outstanding_requests(int(request_id))
        if result:
            (http_request, lock) = result
        else:
            self.__logger.info('Cannot find message entry with request_id ' + request_id)
            return
        
        test_string = dso.get_dst(subject=dsconstants.SUBJECT_MESSAGE_CONTENT, 
                                 predicate=dsconstants.PREDICATE_ECHO_TEST_STRING).get_literal()

        if test_string is None:
            test_string = ""
                                                     
        relative_path = "../../../pages/im/connectivity-test-result.html"
        
        # get the html template
        template_path = os.path.join(os.path.dirname(__file__), relative_path)
        file = open(template_path)
        html_template_string = file.read()
        html_string = html_template_string.replace('${result}', test_string)
        new_dso = dsofactory.create_dso_for_im_http_response_handler(httplib.OK, 
                                                               'text/html', 
                                                               html_string, 
                                                               http_request, 
                                                               lock)
        event = Event(eventconstants.HTTP_RESPONSE_EVENT_TYPE, new_dso)
        EventManager().add_event(event)

class ConnectivityTestWebServiceResponseHandler(BaseEventHandler):
    
    def do(self, data):
        dso = data
                
        web_service_response_dst = dso.get_dst(subject=dsconstants.SUBJECT_ROOT, 
                predicate=dsconstants.PREDICATE_WEB_SERVICE_RESPONSE)
        web_service_response = web_service_response_dst.get_literal()

        message_request_node_dst = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_MESSAGE_REQUEST_NODE)
        
        message_request_node = message_request_node_dst.get_literal()

        echo_resp = ElementTree.XML(web_service_response)
        echo_return = echo_resp.find('.//{http://was6.webservice.spear.lx.dse.vic.gov.au}echoReturn').text

        request_id_dst = dso.get_dst(subject=dsconstants.SUBJECT_ROOT, 
                predicate=dsconstants.PREDICATE_REQUEST_ID)
        
        message_content_dst = DST(subject=dsconstants.SUBJECT_ROOT,
            predicate=dsconstants.PREDICATE_MESSAGE_CONTENT,
            object=dsconstants.SUBJECT_MESSAGE_CONTENT)

        echo_test_string_dst = DST(subject=dsconstants.SUBJECT_MESSAGE_CONTENT,
            predicate=dsconstants.PREDICATE_ECHO_TEST_STRING,
            literal=echo_return)
        
        message_destination_node_dst = DST(subject=dsconstants.SUBJECT_ROOT,
            predicate=dsconstants.PREDICATE_MESSAGE_DESTINATION_NODE,
            literal=message_request_node)

        message_type_dst = DST(subject=dsconstants.SUBJECT_ROOT, 
            predicate=dsconstants.PREDICATE_MESSAGE_TYPE, 
            literal=messagetypeconstants.CONNECTIVITY_TEST_RESPONSE_MESSAGE_TYPE)
                        
        new_dsts = [request_id_dst,
                message_content_dst,
                echo_test_string_dst,
                message_destination_node_dst,
                message_type_dst]
        
        new_dso = DSO(dsts=new_dsts)
        event = Event(eventconstants.SEND_TO_HUB_EVENT_TYPE, new_dso)
        EventManager().add_event(event)